from sys import stdin
import random
from datetime import datetime

#AVRTM 2014 Versión del programa de Andrew Dornbus (code.google.com/p/my-little-tic-tac-toe/)

def adding_one(n):
	return n+1

class Board:
	def __init__(self):
		# Inicia board en una rejilla 3×3 vacía
		self._board = [[0 for i in range(3)] for j in range(3)]
		
	def is_cell_empty(self, x, y):
		return self._board[x][y] == 0
		
	def is_valid_cell(self, x, y):
		return not (x < 0 or x >= 3 or y < 0 or y >= 3)
		
	def choose_square(self, player, x, y):
		self._board[x][y] = player
	
	def print_board(self):
		# Imprime el tablero
		for row in range(3):
			linea1=""
			linea2=""
			linea3=""
			if row == 0:
				for col in range(3):
					if self._board[row][col] == 1:
						if col == 0:
							linea1+="┌─┬"
							linea2+="│"+'\033[0;33m❌\033[0;m'+"│"
							linea3+="├─┼"

						elif col == 1:
							linea1+="─┬"
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┼"
						else:
							linea1+="─┐"
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┤"
					elif self._board[row][col] == 2:
						if col == 0:
							linea1+="┌─┬"
							linea2+="│⭘│"
							linea3+="├─┼"

						elif col == 1:
							linea1+="─┬"
							linea2+="⭘│"
							linea3+="─┼"
						else:
							linea1+="─┐"
							linea2+="⭘│"
							linea3+="─┤"
					else:
						if col == 0:
							linea1+="┌─┬"
							linea2+="│ │"
							linea3+="├─┼"

						elif col == 1:
							linea1+="─┬"
							linea2+=" │"
							linea3+="─┼"
						else:
							linea1+="─┐"
							linea2+=" │"
							linea3+="─┤"
						
					
				print(linea1)
				print(linea2)
				print(linea3)

			elif row == 1:
				for col in range(3):
					if self._board[row][col] == 1:
						if col == 0:
							linea2+="│"+'\033[0;33m❌\033[0;m'+"│"
							linea3+="├─┼"

						elif col == 1:
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┼"
						else:
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┤"
					elif self._board[row][col] == 2:
						if col == 0:
							linea2+="│⭘│"
							linea3+="├─┼"

						elif col == 1:
							linea2+="⭘│"
							linea3+="─┼"
						else:
							linea2+="⭘│"
							linea3+="─┤"
					else:
						if col == 0:
							linea2+="│ │"
							linea3+="├─┼"

						elif col == 1:
							linea2+=" │"
							linea3+="─┼"
						else:
							linea2+=" │"
							linea3+="─┤"

						
				print(linea2)
				print(linea3)

			else:
				for col in range(3):
					if self._board[row][col] == 1:
						if col == 0:
							linea2+="│"+"\033[0;33m❌\033[0;m"+"│"
							linea3+="└─┴"

						elif col == 1:
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┴"
						else:
							linea2+='\033[0;33m❌\033[0;m'+"│"
							linea3+="─┘"
					elif self._board[row][col] == 2:
						if col == 0:
							linea2+="│⭘│"
							linea3+="└─┴"

						elif col == 1:
							linea2+="⭘│"
							linea3+="─┴"
						else:
							linea2+="⭘│"
							linea3+="─┘"
					else:
						if col == 0:
							linea2+="│ │"
							linea3+="└─┴"

						elif col == 1:
							linea2+=" │"
							linea3+="─┴"
						else:
							linea2+=" │"
							linea3+="─┘"

						
				print(linea2)
				print(linea3)

		print("")
	
	def find_winner(self):
		"""Devuelve 0 si hay tablas, 1 si player1 gana, y 2 si player2 gana"""
		
		p1_owns_top_row = self._board[0][0] == 1 and self._board[0][1] == 1 and self._board[0][2] == 1
		p1_owns_mid_row = self._board[1][0] == 1 and self._board[1][1] == 1 and self._board[1][2] == 1
		p1_owns_bot_row = self._board[2][0] == 1 and self._board[2][1] == 1 and self._board[2][2] == 1
		p1_owns_rd_diag = self._board[0][0] == 1 and self._board[1][1] == 1 and self._board[2][2] == 1
		p1_owns_ld_diag = self._board[0][2] == 1 and self._board[1][1] == 1 and self._board[2][0] == 1
		p1_owns_r_col = self._board[0][0] == 1 and self._board[1][0] == 1 and self._board[2][0] == 1
		p1_owns_mid_col = self._board[0][1] == 1 and self._board[1][1] == 1 and self._board[2][1] == 1
		p1_owns_l_col = self._board[0][2] == 1 and self._board[1][2] == 1 and self._board[2][2] == 1
		p2_owns_top_row = self._board[0][0] == 2 and self._board[0][1] == 2 and self._board[0][2] == 2
		p2_owns_mid_row = self._board[1][0] == 2 and self._board[1][1] == 2 and self._board[1][2] == 2
		p2_owns_bot_row = self._board[2][0] == 2 and self._board[2][1] == 2 and self._board[2][2] == 2
		p2_owns_rd_diag = self._board[0][0] == 2 and self._board[1][1] == 2 and self._board[2][2] == 2
		p2_owns_ld_diag = self._board[0][2] == 2 and self._board[1][1] == 2 and self._board[2][0] == 2
		p2_owns_r_col = self._board[0][0] == 2 and self._board[1][0] == 2 and self._board[2][0] == 2
		p2_owns_mid_col = self._board[0][1] == 2 and self._board[1][1] == 2 and self._board[2][1] == 2
		p2_owns_l_col = self._board[0][2] == 2 and self._board[1][2] == 2 and self._board[2][2] == 2

		if p1_owns_top_row or p1_owns_mid_row or p1_owns_bot_row or p1_owns_rd_diag or p1_owns_ld_diag or p1_owns_r_col or p1_owns_mid_col or p1_owns_l_col:
			return 1 
		elif p2_owns_top_row or p2_owns_mid_row or p2_owns_bot_row or p2_owns_rd_diag or p2_owns_ld_diag or p2_owns_r_col or p2_owns_mid_col or p2_owns_l_col:
			return 2 
		else:
			return 0	
	
if __name__ == '__main__':
	print('\033[1;31m 3 EN RAYA\033[0;m\n')
	print('Tablero vacio creado\n')
	contador=0	
	current_player = 0
	board = Board()
	board.print_board()
	while current_player not in (1,2):
		tiempo=datetime.now()
		kstr=str(tiempo.minute)+str(tiempo.second)+str(tiempo.microsecond)
		k=int(kstr)
		random.seed(k)	
		current_player = random.getrandbits(2)	
	while board.find_winner() == 0:
		print('¿Dónde te gustaria colocar tu ficha, Jugador', current_player, '\b?')
		line = input()
		print("")
		coords = line.split()
		if len(coords) != 2:
			print('Por favor introduce un valor para la fila y otro para la columna, entre 1 y 3, separados por un espacio')
			continue
			
		# Convertir a las coordenadas '0-1-2' que necesita la clase «Board»
		x = int(coords[0]) - 1
		y = int(coords[1]) - 1
		if not board.is_valid_cell(x, y):
			print('Has introducido unas coordenadas invalidas', x + 1, y + 1)
			continue
		if not board.is_cell_empty(x, y):
			print('Por favor, elige una casilla vacia, Jugador', current_player)
			continue
			
		board.choose_square(current_player, x, y)
		current_player = (current_player % 2) + 1
		board.print_board()
		contador += 1
		if contador == 9 and board.find_winner() == 0:
			print("Hay tablas\n")
			break

	if board.find_winner() != 0:
		print('¡El Jugador', (current_player % 2) + 1, 'es el ganador!\n')

